
unit clsOperador;

interface

uses Dm,dbtables,biblio;

type TVetor_Operador= array of string;
type TOperador =class(TObject)
  public
    Nome_ope    : string;
    Salario     : double;
    Outros_Rend : double;
    Num_HorasMes: double;
    cod_maquina : string;
    function  Retorna_Ultimo                 : string;
    function  Recuperar(P_codigo_Ope: string): boolean;
    function  Retorna_MaxHoraTrabalho        : double;
    procedure Listar_Operadores(p_Cod_Maquina: string; var vetor: TVetor_Operador);
    procedure Gravar   (P_codigo_Ope: string);
    procedure Excluir  (P_codigo_Ope: string);
    procedure Consultar(P_nome_ope  : string; var p_q:TQuery);
  end;

implementation

{ TOperador }

procedure TOperador.Consultar;
begin
 p_q.sql.Clear;
 p_q.Params.Clear;
 p_q.sql.add('Select * from Operador where Nome_Ope>=:k_nome_ope order by Nome_Ope');
 p_q.parambyname('k_nome_ope').AsString:= P_nome_ope;
end;

procedure TOperador.Excluir;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Delete from Operador where Codigo_Ope=:K_codigo_ope');
 tq.ParamByName('K_codigo_ope').AsString:= strzero(P_codigo_Ope,3);
 tq.ExecSQL;
 tq.Close;
 tq.free;
end;

procedure TOperador.Gravar;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from Operador where Codigo_Ope=:K_codigo_ope');
 tq.ParamByName('K_codigo_ope').AsString := strzero(P_codigo_Ope,3);
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Insert into Operador values(:K_codigo_ope,:K_nome_ope,:k_salario,'+
             ':k_outros_rend,:k_Num_HorasMes,:k_Cod_Maquina)');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Update Operador set Nome_Ope=:K_nome_Ope,Salario=:k_salario,'+
             'Outros_Rend=:k_outros_rend,Num_HorasMes=:k_Num_HorasMes,'+
             'Cod_Maquina=:k_Cod_Maquina where codigo_ope=:K_codigo_ope');
 end;
 tq.ParamByName('K_codigo_ope').AsString := strzero(P_codigo_Ope,3);
 tq.ParamByName('K_nome_ope').AsString   := Nome_ope;
 tq.ParamByName('k_salario').Asfloat     := Salario;
 tq.ParamByName('k_outros_rend').Asfloat := Outros_Rend;
 tq.ParamByName('k_Num_HorasMes').Asfloat:= Num_HorasMes;
 tq.ParamByName('k_Cod_Maquina').AsString:= cod_maquina;
 tq.ExecSQL;
 tq.close;
 tq.free;
end;

procedure TOperador.Listar_Operadores;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select codigo_ope from Operador where Cod_Maquina=:a');
 tq.params[0].AsString:= p_Cod_Maquina;
 tq.open;
 setlength(vetor,0);
 while not tq.eof do begin
  setlength(vetor,length(vetor)+1);
  vetor[length(vetor)-1]:= tq.fieldbyname('codigo_ope').AsString;
  tq.next;
 end;
 tq.close;
 tq.free;
end;

function TOperador.Recuperar;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from Operador where Codigo_Ope=:K_codigo_ope');
 tq.ParamByName('K_codigo_ope').AsString:= strzero(P_codigo_Ope,3);
 tq.open;
 if not tq.eof then result:= true else result:= false;
 Nome_ope    := tq.fieldbyname('Nome_ope').AsString;
 Salario     := tq.fieldbyname('Salario').Asfloat;
 Outros_Rend := tq.fieldbyname('Outros_Rend').Asfloat;
 Num_HorasMes:= tq.fieldByName('Num_HorasMes').Asfloat;
 cod_maquina := tq.fieldByName('Cod_Maquina').AsString;
 tq.close;
 tq.free;
end;

function TOperador.Retorna_MaxHoraTrabalho;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select max(Num_HorasMes) num_horas from Operador');
 tq.open;
 result:= tq.fieldbyname('num_horas').asfloat;
 tq.close;
 tq.free;
end;

function TOperador.Retorna_Ultimo;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select max(Codigo_Ope) ultimo from Operador');
 tq.open;
 if tq.fieldbyname('ultimo').IsNull then
  result:= '001'
 else
  result:= strzero(tq.fieldbyname('ultimo').asinteger+1,3);
 tq.close;
 tq.free;
end;

end.
